<template>
  <div class="nav-search">
    
    <div class="bottom-fb" v-show="isWx">
      <span><van-icon name="arrow-left" size="28px" color="#ccc" @click="goback" /></span>
      <span><van-icon name="arrow" size="28px" color="#ccc" @click="goForward"/></span> 
    </div>

    <div class="nav-search-boxs">
    	<div class="nav-search-box">    
        <div class="nav-search-switch">
          <div class="nav-search-switch-bg" @click="toggle()"></div>
        </div>
        <div class="nav-search-input">
          <div class="item-up">
            <van-cell-group>
              <van-field v-model="start.name" placeholder="请输入起点" @input="inputStart" @focus="focusStart" clearable >
                <span slot="left-icon" class="start-bg"></span>
              </van-field>
            </van-cell-group>
          </div>
          <div class="item-down">
            <van-cell-group>
              <van-field  v-model="end.name" :placeholder="isQuery ? '请输入终点':'请选择下面地点'" @input="inputEnd" @focus="focusEnd" clearable  left-icon="contact">
                <span slot="left-icon" class="end-bg"></span>
              </van-field>
            </van-cell-group>
          </div>
        </div>
  		</div>
      <div class="map-button">
        <van-button type="default" size="small" round=true @click="gotoNavMap" :disabled="buttonStatus" class="button-status">模拟导航</van-button>
      </div>
    </div>
   
    <div v-show="keyShow" class="auto-complete" >
      <div class="tip" v-show="noSearch">没有找到，你是不是查找下面的地址</div>
      <div class="list" v-for="(item, index) in autoList" v-bind:key="index" @click="selectKey(item)">
        <span class="auto-left"><span class="icon"></span><span>{{item.name}}</span></span><span class="auto-right">{{item.mapname}}-{{item.floorname}}</span>
      </div>
    </div>
    
    <div class="history" v-show="!keyShow">
  		<div class="nav-search-tip">
  			<span class="tip-left">历史搜索</span><span class="tip-right" @click="clearKeyword">清空</span>
  		</div>
  		<div class="history-keywords">
  			<div class="no-history" v-show="list ==''">无历史搜索</div>
  			<div class="keywords" v-show="list !==''">
  				<ul> 
  				  <li v-for="(item, index) in list" v-bind:key="index" @click="selectKey(item)">
              <span class="keyword">{{item.name}}-{{item.mapname}}-{{item.floorname}}</span><span class="clear"><van-icon name="cross" @click="clearOne(index)"/></span>
            </li>
  				</ul> 
  			</div>
  		</div>
    </div>
  
  </div>
</template>
<script>
import API from "./api";
import i18n from "./locale";
import { getLocalObjStore, setLocalObjStore } from "@/libs/util";
export default {
  name: "nav-search",
  data() {
    return {
      isWx: false,
      i18n: i18n,
      depaid: 100001,
      type: "end",
      keyShow: false,
      noSearch: false,
      start: {
        name: "",
        query: ""
      },
      end: {
        name: "",
        query: ""
      },
      keyword: "",
      list: "",
      autoList: ""
    };
  },

  mounted() {
    this.initCheckEnvWx();
    this.list = this.getKeyword();
    this.throttleFunc = _.throttle(this.getKeywordList, 1000);
  },

  activated() {
    let name = this.$route.query.name,
      id = this.$route.query.id,
      floorname = this.$route.query.floorname;
    if (id) {
      this.end = { name: name, id: id, floorname: floorname };
    }
    this.keyShow = false;
    if (!id) {
      this.end = { query: name };
      this.inputEnd();
    }
  },

  watch: {
    start: {
      handler(newValue, oldValue) {
        if (newValue.name == "") {
          this.keyShow = false;
        }
      },
      deep: true
    },
    end: {
      handler(newValue, oldValue) {
        if (newValue.name == "") {
          this.keyShow = false;
        }
      },
      deep: true
    }
  },

  computed: {
    isQuery: function() {
      let bool = true;
      if (this.keyShow) {
        bool = false;
      } else {
        bool = true;
      }
      return bool;
    },
    buttonStatus: function() {
      let bool = true;
      if (this.end.name && this.end.id && this.start.name && this.start.id) {
        bool = false;
      } else {
        bool = true;
      }
      return bool;
    }
  },

  methods: {
    onSearch() {
      console.log("onSearch");
    },

    onCancel() {
      console.log("onCancel");
    },

    initCheckEnvWx() {
      let tthis = this;
      if (wx) {
        wx.miniProgram.getEnv(function(res) {
          console.log(162, tthis.isWx, res.miniprogram);
          tthis.isWx = res.miniprogram;
        });
      }
    },

    goback() {
      this.$router.go(-1);
    },

    goForward() {
      this.$router.go(1);
    },

    getKeyword() {
      let data = "",
        arr = getLocalObjStore("keywords");
      if (arr) {
        data = arr;
      }
      return data;
    },

    setKeyword(obj) {
      let arr = this.getKeyword();
      if (!arr) {
        arr = [];
      }
      if (arr && arr.length > 100) {
        arr.pop();
      }
      arr.unshift(obj);
      arr = _.uniqBy(arr, "name");
      setLocalObjStore("keywords", arr);
      this.list = this.getKeyword();
    },

    clearKeyword() {
      localStorage.removeItem("keywords");
      this.list = this.getKeyword();
    },

    clearOne(index) {
      let arr = this.getKeyword();
      arr.splice(index, 1);
      setLocalObjStore("keywords", arr);
      this.list = this.getKeyword();
    },

    focusStart() {
      this.type = "start";
    },

    focusEnd() {
      this.type = "end";
    },

    inputStart() {
      this.type = "start";
      delete this.start.id;
      this.throttleFunc();
    },

    inputEnd() {
      this.type = "end";
      delete this.end.id;
      this.throttleFunc();
    },

    getKeywordList() {
      let key,
        depaid = this.depaid;
      if (this.type == "start") {
        key = this.start.name ? this.start.name : this.start.query;
        delete this.start.query;
      } else {
        key = this.end.name ? this.end.name : this.end.query;
        delete this.end.query;
      }
      this.noSearch = false;
      if (key) {
        API.getSearchList({ depaid: depaid, key: key })
          .then(res => {
            if (res.code == 200) {
              let data = res.data;
              if (data.length > 0) {
                this.autoList = data;
                this.keyShow = true;
              } else {
                //再次请求
                this.getNoSearch(depaid);
              }
            }
          })
          .catch(err => {
            this.$notify("请求异常！");
          });
      } else {
        this.autoList = "";
      }
    },

    gotoNavMap() {
      let startid = this.start.id,
        endid = this.end.id;
      if (this.start.name == "" || this.end.name == "") {
        this.$toast("输入不能为空");
        return;
      }

      if (startid && endid) {
        this.setKeyword(this.start);
        this.setKeyword(this.end);
        window.location.href =
          "http://hqn.fun:16812/Router/Route?startid=" +
          startid +
          "&endid=" +
          endid;
      } else {
        this.$toast("未查到结果");
      }
    },

    toggle() {
      let temp = this.start;
      this.start = this.end;
      this.end = temp;
    },

    selectKey(obj) {
      this.keyShow = false;
      this.autoList = "";
      if (this.type == "start") {
        this.start = {
          name: obj.name,
          id: obj.id,
          floorname: obj.floorname
        };
      } else {
        this.end = {
          name: obj.name,
          id: obj.id,
          floorname: obj.floorname
        };
      }
    },

    getNoSearch(depaid) {
      API.getSearchNoList({ depaid: depaid })
        .then(res => {
          let data = res.data;
          if (data.length > 0) {
            this.autoList = data;
            this.keyShow = true;
            this.noSearch = true;
          }
        })
        .catch(err => {
          this.$notify("请求异常！");
        });
    },

    goBack() {
      this.$router.push({
        name: "nav-home"
      });
    }
  }
};
</script>
<style lang="less">
.nav-search {
  .nav-search-boxs {
    padding: 0 10px;
  }

  .van-search .van-cell {
    border-radius: 15px;
  }

  .van-cell {
    padding: 5px 5px;
  }

  .nav-search-box {
    margin-top: 10px;
    height: 80px;
    border-bottom: 0px solid #ccc;
  }

  .nav-search-tip {
    height: 50px;
    font-size: 13px;
    line-height: 50px;
    .tip-left {
      display: inline-block;
      float: left;
      margin-left: 20px;
    }
    .tip-right {
      display: inline-block;
      width: 50px;
      float: right;
    }
  }

  .auto-complete {
    font-size: 13px;
    background: #ffffff;
    z-index: 1;
    position: absolute;
    width: 100%;
    top: 90px;
    min-height: 100px;
    .list {
      border-bottom: 1px solid #ccc;
      margin: 0 10px;
      height: 40px;
      line-height: 40px;
      overflow: hidden;
      .auto-left {
        float: left;
        margin-left: 20px;
        vertical-align: middle;
        span {
          vertical-align: middle;
        }
        .icon {
          margin-right: 10px;
          display: inline-block;
          width: 18px;
          height: 20px;
          background-image: url("./assets/images/address.png");
          background-position: center;
          background-repeat: no-repeat;
          background-size: 100% 100%;
        }
      }
      .auto-right {
        float: right;
        margin-right: 20px;
      }
    }
  }

  .auto-complete-start {
    top: 50px;
  }

  .auto-complete-end {
    top: 100px;
  }

  .history-keywords {
    font-size: 13px;
    color: #999999;
    .no-history {
      text-align: center;
      margin-bottom: 20px;
    }
    .keywords {
      margin-top: 10px;
      display: inline;
      line-height: 28px;
      height: auto;
      word-break: break-all;
      word-wrap: break-word;
      li {
        float: left;
        margin: 3px 10px;
        padding: 0 10px;
        background: #ececec;
        border-radius: 20px;
        line-height: 25px;
        height: 25px;
        .clear {
          margin-left: 10px;
          display: inline-block;
          width: 20px;
          text-align: center;
          vertical-align: middle;
        }
      }
    }
  }

  .map-button {
    text-align: center;
    padding: 0 16px;
    margin: 10px 0px;
    .van-button {
      width: 100%;
      background: #7658d2;
      color: #fff;
      height: 40px;
    }
  }

  .nav-search-input {
    margin-left: 20px;
    margin-right: 80px;
    .item-up {
      border-bottom: 1px solid #ccc;
    }
    .item-down {
      border-bottom: 1px solid #ccc;
    }
  }

  .nav-search-switch {
    float: right;
    width: 80px;
    height: 70px;
    position: relative;
    .nav-search-switch-bg {
      margin: auto;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      position: absolute;
      width: 25px;
      height: 25px;
      background-image: url("./assets/images/switch.png");
      background-position: center;
      background-repeat: no-repeat;
      background-size: 100% 100%;
    }
  }

  .van-hairline--top-bottom::after {
    border-width: 0px 0;
  }

  .start-bg {
    margin-top: 4px;
    margin-right: 10px;
    width: 15px;
    height: 15px;
    background-image: url("./assets/images/start.png");
    background-position: center;
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }

  .end-bg {
    margin-top: 4px;
    margin-right: 10px;
    width: 15px;
    height: 15px;
    background-image: url("./assets/images/end.png");
    background-position: center;
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }

  .tip {
    color: #ccc;
    text-align: center;
  }

  .input-like-color {
    color: #a8a8a8;
  }

  .button-status {
    font-size: 15px;
  }

  .bottom-fb {
    text-align: center;
    position: fixed;
    bottom: 0;
    height: 30px;
    border-top: 1px solid #e6e6e6;
    width: 100%;
    padding: 5px;
    color: #999;
    span {
      margin: 0 20px;
    }
  }
}
</style>
